﻿
namespace Sai.Core.Reporting
{
    using System;
    using System.Collections.Generic;

	public class FileBasedReportTransformationRepository : IReportTransformationRepository
    {
        private Dictionary<TypeFormatPair, string> typeFileMap = new Dictionary<TypeFormatPair, string>();

        public FileBasedReportTransformationRepository(string folder)
        {
            this.Folder = folder;

            this.typeFileMap.Add(new TypeFormatPair(typeof(IProductReleaseDifference), ReportFormat.Html), "HtmlSummary.xslt");
            this.typeFileMap.Add(new TypeFormatPair(typeof(IAssemblyDifference), ReportFormat.Html), "HtmlAssembly.xslt");
            this.typeFileMap.Add(new TypeFormatPair(typeof(ITypeDifference), ReportFormat.Html), "HtmlType.xslt");
            this.typeFileMap.Add(new TypeFormatPair(typeof(ITypeListing), ReportFormat.Html), "HtmlListing.xslt");

            this.typeFileMap.Add(new TypeFormatPair(typeof(IProductReleaseDifference), ReportFormat.Html4), "Html4Summary.xslt");
            this.typeFileMap.Add(new TypeFormatPair(typeof(IAssemblyDifference), ReportFormat.Html4), "Html4Assembly.xslt");
            this.typeFileMap.Add(new TypeFormatPair(typeof(ITypeDifference), ReportFormat.Html4), "Html4Type.xslt");
            this.typeFileMap.Add(new TypeFormatPair(typeof(ITypeListing), ReportFormat.Html4), "Html4Listing.xslt");

            this.typeFileMap.Add(new TypeFormatPair(typeof(IProductReleaseDifference), ReportFormat.Maml), "MamlSummary.xslt");
            this.typeFileMap.Add(new TypeFormatPair(typeof(IAssemblyDifference), ReportFormat.Maml), "MamlAssembly.xslt");
            this.typeFileMap.Add(new TypeFormatPair(typeof(ITypeDifference), ReportFormat.Maml), "MamlType.xslt");
            this.typeFileMap.Add(new TypeFormatPair(typeof(ITypeListing), ReportFormat.Maml), "MamlListing.xslt");

            this.typeFileMap.Add(new TypeFormatPair(typeof(IProductReleaseDifference), ReportFormat.Xml), "XmlSummary.xslt");
            this.typeFileMap.Add(new TypeFormatPair(typeof(IAssemblyDifference), ReportFormat.Xml), "XmlAssembly.xslt");
            this.typeFileMap.Add(new TypeFormatPair(typeof(ITypeDifference), ReportFormat.Xml), "XmlType.xslt");
            this.typeFileMap.Add(new TypeFormatPair(typeof(ITypeListing), ReportFormat.Xml), "XmlListing.xslt");
        }

        private string Folder { get; set; }

        public IReportTransformation Find(Type objectType, ReportFormat format)
        {
            // map object to stylesheet
            TypeFormatPair candidateKey = new TypeFormatPair(objectType, format);

            if (this.typeFileMap.ContainsKey(candidateKey))
            {
                string candidateStyleSheet = System.IO.Path.Combine(this.Folder, this.typeFileMap[candidateKey]);

                if (System.IO.File.Exists(candidateStyleSheet))
                {
                    return new FileBasedReportTransformation(candidateStyleSheet);
                }
            }

            return null;
        }
    }
}
